package com.ruoyi.web.controller.module.order;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.customer.domain.CustomerUser;
import com.ruoyi.customer.service.ICustomerUserService;
import com.ruoyi.market.domain.ConfigPay;
import com.ruoyi.market.domain.ConfigProgram;
import com.ruoyi.market.service.IConfigPayService;
import com.ruoyi.market.service.IConfigProgramService;
import com.ruoyi.order.domain.OrderWallet;
import com.ruoyi.order.service.IOrderWalletService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 充值订单信息
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/order/wallet")
public class OrderWalletController extends BaseController
{
    @Autowired
    private IOrderWalletService orderWalletService;

    @Autowired
    private ICustomerUserService customerUserService;

    @Autowired
    private IConfigPayService configPayService;

    @Autowired
    private IConfigProgramService configProgramService;

    /**
     * 获取充值订单列表
     */
    @PostMapping("/orderWalletList")
    public TableDataInfo list(@RequestBody OrderWallet orderWallet, HttpServletRequest request)
    {
        String shopId = request.getHeader("ShopId"); // 店铺id
        if(shopId != null && !shopId.equals("")){
            orderWallet.setShopId(shopId);
        }
        startPageByPost();
        List<OrderWallet> list = orderWalletService.selectOrderWalletList(orderWallet);
        return getDataTable(list);
    }

    @Log(title = "充值订单管理 - 导出", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('order:wallet:export')")
    @PostMapping("/orderWalletExport")
    public void export(HttpServletResponse response, OrderWallet orderWallet)
    {
        List<OrderWallet> list = orderWalletService.selectOrderWalletList(orderWallet);
        ExcelUtil<OrderWallet> util = new ExcelUtil<OrderWallet>(OrderWallet.class);
        util.exportExcel(response, list, "充值订单数据");
    }

    /**
     * 根据充值订单编号获取详细信息
     */
    @GetMapping(value = { "/", "/{orderNo}" })
    public AjaxResult getInfo(@PathVariable(value = "orderNo", required = false) String orderNo)
    {
        AjaxResult ajax = AjaxResult.success();
        if (StringUtils.isNotNull(orderNo))
        {
            OrderWallet orderWallet = orderWalletService.selectOrderWalletByOrderNo(orderNo);
            ajax.put(AjaxResult.DATA_TAG, orderWallet);
        }
        return ajax;
    }

    /**
     * 新增充值订单
     */
    @PostMapping("/orderWalletCreate")
    public AjaxResult orderWalletCreate(@Validated @RequestBody OrderWallet orderWallet, HttpServletRequest request)
    {
        String shopId = request.getHeader("ShopId"); // 店铺id
        if(shopId != null && !shopId.equals("")){
            orderWallet.setShopId(shopId);
        }
        Long userId = SecurityUtils.getUserId();
        CustomerUser cu = customerUserService.selectUserById(userId);
        customerUserService.checkUserAllowed(cu);
        AjaxResult ajax = AjaxResult.success();

        ConfigPay configPay = configPayService.selectConfigPayByShopId(shopId);
        ConfigProgram configProgram = configProgramService.selectConfigProgramByShopId(shopId);
        ajax.put(AjaxResult.DATA_TAG, orderWalletService.insertOrderWallet(orderWallet));
        ajax.put("configPay", configPay);
        ajax.put("configProgram", configProgram);
        return ajax;
    }

    /**
     * 修改充值订单
     */
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody OrderWallet orderWallet)
    {
        return toAjax(orderWalletService.updateOrderWallet(orderWallet));
    }

    /**
     * 删除充值订单
     */
    @PreAuthorize("@ss.hasPermi('order:wallet:remove')")
    @Log(title = "充值订单管理 - 删除充值订单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{orderNos}")
    public AjaxResult remove(@PathVariable String[] orderNos)
    {
        return toAjax(orderWalletService.deleteOrderWalletByOrderNos(orderNos));
    }
}
